# @Time : 2025-04-07 11:18
# @Author : Fioman 
# @Phone : 13149920693
"""
视觉帮助类
"""
"""
视觉处理的帮助类,主要是抽离出来一些公共的方法,供其他模块调用,这样其他模块在使用的时候
就可以只关注自己需要关注的地方,而不是一直在那里拉来拉去的,不太好控制.一定要尽量的简化
主流程,这样有利于代码的维护和扩展.
"""
import os.path

import cv2 as cv

from vision_process.return_model import DataResult

showState = False  # 是否显示图像
calcThresBigGlobal = 20
calcThresSmallGlobal = 9
boardBreak = False
mtx = [
    [0, 1, 0],
    [1, 0, 0]
]

filterArea = 5000
sizeScale = 6.5
colorWhite = (255, 255, 255)
colorBlue = (255, 0, 0)
colorGreen = (0, 255, 0)
colorRed = (0, 0, 255)
colorPink = (255, 0, 255)


def create_image_test_dir(filePath) -> DataResult:
    """
    根据文件路径创建测试的时候需要的目录.
    @param filePath:
    @return:
    """
    result = DataResult()
    try:
        if not os.path.exists(filePath):
            # 如果路径不存在,报错
            result.info = f"{filePath} 路径不存在,文件路径错误"
            return result
        # 先判断是目录还是文件
        keepDir = filePath
        if not os.path.isdir(keepDir):
            keepDir, _ = os.path.split(keepDir)

        keepOkPath = os.path.join(keepDir, "ok")
        keepResPath = os.path.join(keepDir, "cutLines")
        keepFailPath = os.path.join(keepDir, "fail")
        keepFirstBestPath = os.path.join(keepDir, "firstBest")

        if not os.path.exists(keepOkPath):
            os.makedirs(keepOkPath)
        if not os.path.exists(keepResPath):
            os.makedirs(keepResPath)
        if not os.path.exists(keepFailPath):
            os.makedirs(keepFailPath)
        if not os.path.exists(keepFirstBestPath):
            os.makedirs(keepFirstBestPath)

        fileNames = []  # 保存要去识别的所有的图片的文件名
        if os.path.isfile(filePath):
            filePath, fileName = os.path.split(filePath)
            fileNames.append(fileName)
        else:
            for root, dirs, files in os.walk(filePath):
                if root != filePath:
                    continue
                for file in files:
                    if file[-4:] != ".bmp":
                        continue
                    fileNames.append(file)
        result.state = True
        result.data = [keepDir, fileNames, keepOkPath, keepResPath, keepFailPath, keepFirstBestPath]
        return result
    except Exception as e:
        result.info = "in create_image_test_dir() error: {}".format(e)
        result.data = [None, [], None, None, None, None]
    return result


def get_contour_is_rect_ratio(cnt):
    """
    获取轮廓的近似度
    :param cnt:
    :return:
    """
    boxMaster = cv.minAreaRect(cnt)
    _, (w, h), _ = boxMaster
    ratio = cv.contourArea(cnt) / (w * h)
    return round(ratio, 5)


if __name__ == '__main__':
    res = create_image_test_dir("")
    if res.state:
        print("创建图像测试目录成功")
    else:
        print(f"创建图像实测实目录失败: {res.info}")
